layui.use(['form','jquery','table','layer'],function(){
    let form = layui.form,
        $ = layui.jquery,
        table = layui.table,
        layer = layui.layer;

    //加载数据表格
    let tabInstance = table.render({
        elem: '#clientList',
        url: '/customer-info/client', //数据接口
        page: true, //开启分页
        limit:5, //每页显示的条数，默认10
        limits:[2,5,10,15,], //每页显示条数选择框数据
        request:{
            pageName: 'current',
            limitName: 'size'
        },
        cols: [[ //表头
            {field: 'customerId', title: '客户编号', width:160,sort:true, fixed: 'left'},
            {field: 'customerName', title: '客户名称'},
            {field: 'city', title: '地区'},
            {field: 'customerMgr', title: '客户经理'},
            {field: 'customerMgrId', title: '客户经理编号'},
            {field: 'customerGrade', title: '客户等级'},
            {field: 'customerSatisfaction', title: '客户满意度'},
            {field: 'customerCredit', title: '客户信用度'},
            {title: '操作', width:300,fixed:'right',toolbar:"#clientTool"}
        ]]
    });
    //搜索监听启用
    form.on('submit(search)',function(data){
        tabInstance.reload({
            where :data.field
        });
        return false;
    });

    //工具栏操作
    table.on('tool(clientList)',function(obj){
        switch (obj.event){
            case 'detail_btn':
                sessionStorage.setItem("customerId",obj.data.customerId);
                layer.open({
                    type:2,
                    title: obj.data.customerName+"客户详细信息",
                    area: ['1200px', '600px'],
                    content:["views/client/client_detail.jsp","no"],
                    btnAlign: 'c',
                    btn:['关闭'],
                });
                break;
            case 'edit_btn':
                sessionStorage.setItem("customerId",obj.data.customerId);
                layer.open({
                    type:2,
                    title: obj.data.customerName+"客户信息编辑界面",
                    area: ['1200px', '632px'],
                    content:["views/client/client_save.jsp","no"],
                    btnAlign: 'c',
                    btn:['提交','关闭'],
                    yes: function(index, layero){
                        //user_editsave
                        let customerInfo_form = layer.getChildFrame('body', index).find("#save");
                        let data = {};
                        //把数组转为JSON
                        customerInfo_form.serializeArray().forEach(function(val){
                            data[val.name] = val.value;
                        });
                        $.post('/customer-info/save',data,function(info){
                            layer.close(index);
                            layer.msg(info.msg);
                            tabInstance.reload();
                        });
                    }
                });
                //编辑
                break;

            case 'customer_contact':
                window.location = 'views/customerContact/list.jsp?id='+obj.data.customerId+'&name='+obj.data.customerName;
                break;
            case 'contact_record':
                window.location = 'views/contact-record/contact-rec.jsp?id='+obj.data.customerId+'&name='+obj.data.customerName;
                break;


        }
    });

});